<?php

namespace App\Http\Controllers;

use App\Logic\ActivityLogic;
use Illuminate\Support\Facades\DB;

/**
 * MonthlyCalendar 日历表信息
 */
class MonthlyCalendar extends Base
{

    public function logic()
    {

        $uid = $this->uid;
        //计算今天日期
        $today = date('Y-m-d');
        $year  = date('Y');
        $month = date('m');
        $day   = date('d');
        //月份
        $year_month = $year . "年" . $month . "月";
        $yearmonth  = $year . $month;

        //本月总天数
        $month_all_days = date("t");

        //计算本月第一天是星期几
        $timestamp           = strtotime($today);
        $sdata               = date('Y-m-d', $timestamp - (date('d', $timestamp) - 1) * 86400);
        $month_first_weekday = date("w", strtotime($sdata));


        $data                        = array();
        $data["today"]               = $today;
        $data["day"]                 = (int)$day;
        $data["year_month"]          = $year_month;
        $data["month_all_days"]      = (int)$month_all_days;
        $data["month_first_weekday"] = (int)$month_first_weekday;

        $row = DB::select(
            "SELECT * FROM t_signin_rec
			WHERE uid=$uid 
			and create_time >DATE_ADD(CURDATE(),INTERVAL -DAY(CURDATE())+1 DAY) 
			AND create_time <DATE_ADD(CURDATE()-DAY(CURDATE())+1,INTERVAL 1 MONTH )"
        );

        $sign_in = array();
        for ($i = 1; $i <= $month_all_days; $i++) {
            $flag = 0;
            for ($j = 0; $j < count($row); $j++) {
                if ((int)$row[$j]->day == $i) {
                    $flag = 1;
                    break;
                }
            }
            $sign_in[] = $flag;
        }
        $data["sign_in"] = $sign_in;

        $box_info = array(
            array('id' => 3, 'get_flag' => 0),
            array('id' => 7, 'get_flag' => 0),
            array('id' => 15, 'get_flag' => 0),
            array('id' => 21, 'get_flag' => 0),
            array('id' => 28, 'get_flag' => 0)
        );

        //暂时定义连续签到的奖品
        $prize_arr = [
            3 => '10002:1|10032:1|10028:1',
            7 => '10003:1|10033:1|10036:1',
            15 => '10004:1|10034:1|10037:1',
            21 => '10005:1|10035:1|10038:1',
            28 => '10031:1|10027:1|10039:1',
        ];



        //查询本月领取宝箱的所有记录
        $row2 = DB::select(
            "SELECT * FROM t_reward_box_rec 
			WHERE uid=$uid 
			AND create_time >DATE_ADD(CURDATE(),INTERVAL -DAY(CURDATE())+1 DAY)
			AND create_time <DATE_ADD(CURDATE()-DAY(CURDATE())+1,INTERVAL 1 MONTH )"
        );
        foreach ($box_info as $key => $value) {
            for ($j = 0; $j < count($row2); $j++) {
                if ((int)$row2[$j]->get_box_type == (int)$value['id']) {
                    $box_info[$key]['get_flag'] = 1;
                    break;
                }
            }
            $rule = $prize_arr[$value['id']];
            $rule = explode('|',$rule);
            $desc = '';
            foreach ($rule as $value) {
                $value_arr = explode(':',$value);
                $desc.=DB::table('t_item_conf')->where('id',$value_arr[0])->value('name');
                $desc.='，';
            }
            $box_info[$key]['prize_desc'] = rtrim($desc,'，');
        }
        $data['box_info']  = $box_info;
        $object            = ActivityLogic::instance()->activityConfInfo(2);
        $data['sign_desc'] = '每日签到可领取：' . ActivityLogic::instance()->ruleContent($object ? $object->rule : '').'每月累计签到达到指定天数，可获得价值300元的额外奖励！单击宝箱查看奖励内容。';
        return $this->returnData($data);
    }

}